/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.clientpackets;


import l1j.opqlo.Server.L1World.WorldClan;
import l1j.server.echo.ClientThread;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.L1Clan;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_Message_YN;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.utils.FaceToFace;

import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * 處理收到由客戶端傳來加入血盟的封包
 */
public class C_JoinClan extends ClientBasePacket {

	private static final String C_JOIN_CLAN = "[C] C_JoinClan";
	private static final Logger _log = Logger.getLogger(C_JoinClan.class.getName());
	@Override
	public String getType() {
		return C_JOIN_CLAN;
	}

	private void JoinClan(L1PcInstance player, L1PcInstance target) {
		
		if (!target.isCrown() && 
				target.getClanRank() != L1Clan.CLAN_RANK_GUARDIAN) { // 如果面對的對象不是王族或守護騎士
			player.sendPackets(new S_ServerMessage(92, target.getName())); // \f1%0はプリンスやプリンセスではありません。
			return;
		}
		if(player.getClanid() == target.getClanid()){
			// 同一血盟
			player.sendPackets(new S_ServerMessage(1199));
			return;
		}

		final int clan_id = target.getClanid();
		final String clan_name = target.getClanname();
		if (clan_id == 0) { // 面對的對象沒有創立血盟
			player.sendPackets(new S_ServerMessage(90, target.getName())); // \f1%0は血盟を創設していない状態です。
			return;
		}

		final L1Clan clan = WorldClan.get().getClan(clan_name);
		if (clan == null) {
			return;
		}

		if (target.getId() != clan.getLeaderId()
				&& 	target.getClanRank() != L1Clan.CLAN_RANK_GUARDIAN) { // 相手が血盟主以外
			player.sendPackets(new S_ServerMessage(92, target.getName())); // \f1%0はプリンスやプリンセスではありません。
			return;
		}

		if (player.getClanid() != 0) { // 既にクランに加入済み
			if (player.isCrown()) { // 自分が君主
				final String player_clan_name = player.getClanname();
				final L1Clan player_clan = WorldClan.get().getClan(
						player_clan_name);
				if (player_clan == null) {
					return;
				}

				if (player.getId() != player_clan.getLeaderId()) { // 自分が血盟主以外
					player.sendPackets(new S_ServerMessage(89)); // \f1あなたはすでに血盟に加入しています。
					return;
				}

				if (player_clan.getCastleId() != 0 || // 自分が城主・アジト保有
						player_clan.getHouseId() != 0) {
					player.sendPackets(new S_ServerMessage(665)); // \f1城やアジトを所有した状態で血盟を解散することはできません。
					return;
				}
			} else {
				player.sendPackets(new S_ServerMessage(89)); // \f1あなたはすでに血盟に加入しています。
				return;
			}
		}

		target.setTempID(player.getId()); // 相手のオブジェクトIDを保存しておく
		target.sendPackets(new S_Message_YN(97, player.getName())); // %0が血盟に加入したがっています。承諾しますか？（Y/N）
	}

	@Override
	public void start(final byte[] decrypt, final ClientThread client) {
		try {
			// 資料載入
			this.read(decrypt);

			final L1PcInstance pc = client.getActiveChar();
			if (pc == null || pc.isGhost()) {
				return;
			}

			final L1Character target = FaceToFace.faceToFace(pc);
			if (target != null && target instanceof L1PcInstance) {
				this.JoinClan(pc, (L1PcInstance) target);
			}
		} catch (final Exception e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			this.over();
		}
	}
}
